<?php

class Contact_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = 'wel_contact';
        $this->load->model("functions_model");
    }

    function delete($tid) {
        $where['tid'] = $tid;
        $data['deleted'] = 1;
        $data['last_modified'] = date("Y-m-d H:i:s");
        $this->db->where($where);
        $this->db->update($this->tableName, $data);
    }

    function newMsg() {
        $data = $_POST;
        $data['last_modified'] = date("Y-m-d H:i:s");
        $this->db->insert($this->tableName, $data);
    }

    function getOne($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->from($this->tableName);
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $row['last_modified'] = $this->functions_model->prettyDateTime($row['last_modified']);
            $result[] = $row;
        }
	
	
        $data['read'] = 1;
	$where['tid'] = $tid;
        $this->db->where($where);
        $this->db->update($this->tableName, $data);
	
        return $result;
    }

    function getAll($field=NULL, $search=NULL, $offset=0, $per_page=1000) {
        if ($field and $search) {
            if ($field == 'all') {
                $this->db->like("email", $search);
                $this->db->or_like("subject", $search);
                $this->db->or_like("description", $search);
            } else {
                $data[$field] = $search;
                $this->db->like($data);
            }
        }
        $this->db->select('tid,email, subject, description,last_modified,read');
        $this->db->order_by("tid", "desc");
        $query = $this->db->get($this->tableName,$per_page, $offset);
        $result = array();
        $count = $offset;
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            $row['last_modified'] = $this->functions_model->prettyDateTime($row['last_modified']);
            $count++;
            $row['tid'] = $count;
            $result[] = $row;
        }
        return $result;
    }

}

?>
